﻿using System;
using Mono.Collections.Generic;

namespace Mono.Cecil.Cil
{
	// Token: 0x02000120 RID: 288
	internal sealed class VariableDefinitionCollection : Collection<VariableDefinition>
	{
		// Token: 0x06000B36 RID: 2870 RVA: 0x00005FAA File Offset: 0x000041AA
		internal VariableDefinitionCollection(MethodDefinition method)
		{
			this.method = method;
		}

		// Token: 0x06000B37 RID: 2871 RVA: 0x00005FBB File Offset: 0x000041BB
		internal VariableDefinitionCollection(MethodDefinition method, int capacity) : base(capacity)
		{
			this.method = method;
		}

		// Token: 0x06000B38 RID: 2872 RVA: 0x00005FCD File Offset: 0x000041CD
		protected override void OnAdd(VariableDefinition item, int index)
		{
			item.index = index;
		}

		// Token: 0x06000B39 RID: 2873 RVA: 0x00005FD7 File Offset: 0x000041D7
		protected override void OnInsert(VariableDefinition item, int index)
		{
			item.index = index;
			this.UpdateVariableIndices(index, 1, null);
		}

		// Token: 0x06000B3A RID: 2874 RVA: 0x00005FCD File Offset: 0x000041CD
		protected override void OnSet(VariableDefinition item, int index)
		{
			item.index = index;
		}

		// Token: 0x06000B3B RID: 2875 RVA: 0x00005FEB File Offset: 0x000041EB
		protected override void OnRemove(VariableDefinition item, int index)
		{
			this.UpdateVariableIndices(index + 1, -1, item);
			item.index = -1;
		}

		// Token: 0x06000B3C RID: 2876 RVA: 0x00031E38 File Offset: 0x00030038
		private void UpdateVariableIndices(int startIndex, int offset, VariableDefinition variableToRemove = null)
		{
			for (int i = startIndex; i < this.size; i++)
			{
				this.items[i].index = i + offset;
			}
			MethodDebugInformation methodDebugInformation = (this.method == null) ? null : this.method.debug_info;
			bool flag = methodDebugInformation == null || methodDebugInformation.Scope == null;
			if (!flag)
			{
				foreach (ScopeDebugInformation scopeDebugInformation in methodDebugInformation.GetScopes())
				{
					bool flag2 = !scopeDebugInformation.HasVariables;
					if (!flag2)
					{
						Collection<VariableDebugInformation> variables = scopeDebugInformation.Variables;
						int num = -1;
						for (int j = 0; j < variables.Count; j++)
						{
							VariableDebugInformation variableDebugInformation = variables[j];
							bool flag3 = variableToRemove != null && ((variableDebugInformation.index.IsResolved && variableDebugInformation.index.ResolvedVariable == variableToRemove) || (!variableDebugInformation.index.IsResolved && variableDebugInformation.Index == variableToRemove.Index));
							if (flag3)
							{
								num = j;
							}
							else
							{
								bool flag4 = !variableDebugInformation.index.IsResolved && variableDebugInformation.Index >= startIndex;
								if (flag4)
								{
									variableDebugInformation.index = new VariableIndex(variableDebugInformation.Index + offset);
								}
							}
						}
						bool flag5 = num >= 0;
						if (flag5)
						{
							variables.RemoveAt(num);
						}
					}
				}
			}
		}

		// Token: 0x040005DC RID: 1500
		private readonly MethodDefinition method;
	}
}
